home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / winterp-1.13 / src-server / w_savedobjs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-05  |  4.6 KB  |  111 lines

  1. /* -*-C-*-
  2. ********************************************************************************
  3. *
  4. * File:         w_savedobjs.c
  5. * RCS:          $Header: w_savedobjs.c,v 1.3 91/03/14 03:14:12 mayer Exp $
  6. * Description:  Hashtable of LVAL's to be protected against garbage coll.
  7. * Author:       Niels Mayer, HPLabs
  8. * Created:      Sun Sep 24 22:31:43 1989
  9. * Modified:     Fri Oct  4 20:18:15 1991 (Niels Mayer) mayer@hplnpm
  10. * Language:     C
  11. * Package:      N/A
  12. * Status:       X11r5 contrib tape release
  13. *
  14. * WINTERP Copyright 1989, 1990, 1991 Hewlett-Packard Company (by Niels Mayer).
  15. * XLISP version 2.1, Copyright (c) 1989, by David Betz.
  16. *
  17. * Permission to use, copy, modify, distribute, and sell this software and its
  18. * documentation for any purpose is hereby granted without fee, provided that
  19. * the above copyright notice appear in all copies and that both that
  20. * copyright notice and this permission notice appear in supporting
  21. * documentation, and that the name of Hewlett-Packard and David Betz not be
  22. * used in advertising or publicity pertaining to distribution of the software
  23. * without specific, written prior permission.  Hewlett-Packard and David Betz
  24. * make no representations about the suitability of this software for any
  25. * purpose. It is provided "as is" without express or implied warranty.
  26. *
  27. * HEWLETT-PACKARD AND DAVID BETZ DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  28. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
  29. * IN NO EVENT SHALL HEWLETT-PACKARD NOR DAVID BETZ BE LIABLE FOR ANY SPECIAL,
  30. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  31. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  32. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  33. * PERFORMANCE OF THIS SOFTWARE.
  34. *
  35. * See ./winterp/COPYRIGHT for information on contacting the authors.
  36. * Please send modifications, improvements and bugfixes to mayer@hplabs.hp.com
  37. * Post XLISP-specific questions/information to the newsgroup comp.lang.lisp.x
  38. **
  39. ********************************************************************************
  40. */
  41. static char rcs_identity[] = "@(#)$Header: w_savedobjs.c,v 1.3 91/03/14 03:14:12 mayer Exp $";
  42.  
  43. #include <stdio.h>
  44. #include <Xm/Xm.h>        /* Xm/Xm.h only needed for "winterp.h"*/
  45. #include "winterp.h"
  46. #include "user_prefs.h"
  47. #include "xlisp/xlisp.h"
  48.  
  49. /*
  50.   We put an initializer for LVAL v_savedobjs in xlsym:xlsinit(), and add
  51.   this to the list of structures that gets marked by gc. v_savedobjs is a
  52.   hashtable for storing LVAL's that need to persist across garbage
  53.   collections.
  54.  
  55.   v_savedobjs is typically used for storing callback-objects,
  56.   timeout-objects, etc.  These objects need to persist even though they are
  57.   not directly referenced by any user-space global variables because a
  58.   callback or timout may occur at any time and we must not allow the code or
  59.   lexical environment associated with one of these delayed calls to be gc'd.
  60.  
  61.   v_savedobjs is used to store LVAL's. The hash function removes the bits
  62.   from the LVAL address used for adressing within the LVAL (i.e. rightshift
  63.   by the # of bits in an LVAL struct. Then we take that value modulo
  64.   VSAVEDOBJS_SIZE to come up with the index into the hashtable.
  65. */
  66.  
  67. #define HASHTAB_ADDR_MASK 0xffL    /* value must be (2^x - 1) for any x */
  68. #define HASHTAB_SIZE ((int) HASHTAB_ADDR_MASK + 1)
  69. static int LVAL_ADDRESS_WIDTH;    /* must be initialized by Wso_Init(). */
  70.  
  71. /******************************************************************************
  72.  * Given an LVAL, returns the hash index into v_savedobjs for that object.
  73.  * This is done quite sleazily/simply by using the adress of the lisp object
  74.  * right shifted by the number of bits used to index inside a LVAL structure
  75.  * Then masked by the size of the power-of-2-sized hashtable. The result is
  76.  * a value ranging from 0 to HASH_MASK.
  77.  ******************************************************************************/
  78. int Wso_Hash(object)
  79.      LVAL object;
  80. {
  81.   unsigned long i;
  82.   i = ((unsigned long) object >> LVAL_ADDRESS_WIDTH) & HASHTAB_ADDR_MASK;
  83.   return ((int) i);
  84. }
  85.  
  86.  
  87. /******************************************************************************
  88.  *
  89.  ******************************************************************************/
  90. Wso_Init()
  91. {
  92.   extern LVAL v_savedobjs;    /* xlglob.c */
  93.   int i;
  94.   LVAL sym;
  95.  
  96.   /* compute the number of bits used to index within an LVAL structure */
  97.   i = sizeof(struct node);    /* size of an LVAL* in bytes */
  98.   LVAL_ADDRESS_WIDTH = 1;
  99.   while (i >= 2) {
  100.     i /= 2;
  101.     ++LVAL_ADDRESS_WIDTH;
  102.   }
  103.  
  104.   sym = xlenter("*SAVED_OBJS*");
  105.   v_savedobjs = newvector(HASHTAB_SIZE);
  106.   setvalue(sym, v_savedobjs); /* allow lisp access to v_savedobjs for debugging */
  107. }
  108.  
  109.  
  110.